users = {}
current_user = None


def register():
    while True:
        username = input("请输入用户名：").strip()
        if not username:
            print("用户名不能为空，请重新输入。")
            continue
        if username in users:
            print("用户名已存在，请重新输入。")
        else:
            break
    while True:
        password = input("请输入密码：").strip()
        if len(password) < 4:
            print("密码长度至少三位，请重新输入。")
        else:
            break
    users[username] = {
        'password': password,
        'balance': 0.0
    }
    print("注册成功！当前余额为0元。")


def login():
    global current_user
    username = input("请输入用户名：").strip()
    password = input("请输入密码：").strip()

    if username not in users:
        print("用户名不存在。")
        return False

    if users[username]['password'] != password:
        print("密码错误。")
        return False

    current_user = username
    print(f"登录成功！当前余额为{users[username]['balance']}元。")
    return True


def deposit():
    while True:
        amount = input("请输入存款金额：").strip()
        try:
            amount = float(amount)
            if amount <= 0:
                print("金额必须大于0，请重新输入。")
            else:
                users[current_user]['balance'] += amount
                print(f"存款成功！当前余额为{users[current_user]['balance']}元。")
                break
        except ValueError:
            print("请输入有效的数字。")


def withdraw():
    while True:
        amount = input("请输入取款金额：").strip()
        try:
            amount = float(amount)
            if amount <= 0:
                print("金额必须大于0，请重新输入。")
            elif amount > users[current_user]['balance']:
                print("余额不足，请重新输入。")
            else:
                users[current_user]['balance'] -= amount
                print(f"取款成功！当前余额为{users[current_user]['balance']}元。")
                break
        except ValueError:
            print("请输入有效的数字。")


def transfer():
    while True:
        recipient = input("请输入收款人用户名：").strip()
        if recipient not in users:
            print(f"{recipient} 该用户名不存在，请重新输入。")
        else:
            break

    while True:
        amount = input("请输入转账金额：").strip()
        try:
            amount = float(amount)
            if amount < 0:
                print("金额不能为负数，请重新输入。")
            elif amount > users[current_user]['balance']:
                print("余额不足，请重新输入。")
            else:
                users[current_user]['balance'] -= amount
                users[recipient]['balance'] += amount
                print(f"转账成功！当前余额为{users[current_user]['balance']}元。")
                break
        except ValueError:
            print("请输入有效的数字。")


def logged_in_menu():
    while True:
        print("\n请选择操作：取款、存款、转账、退出登录")
        action = input().strip()
        if action == "退出登录":
            global current_user
            current_user = None
            print("已退出登录。")
            break
        elif action == "存款":
            deposit()
        elif action == "取款":
            withdraw()
        elif action == "转账":
            transfer()
        else:
            print("无效操作，请重新输入。")


def main():
    print("欢迎使用华测 ATM")
    while True:
        if current_user is None:
            print("\n请输入需要进行的操作，例如：注册、登录、退出")
            action = input().strip()
            if action == "退出":
                print("感谢使用，再见！")
                break
            elif action == "注册":
                register()
            elif action == "登录":
                if login():
                    logged_in_menu()
            else:
                print("无效操作，请重新输入。")
        else:
            logged_in_menu()


if __name__ == "__main__":
    main()
